// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    hiddenModal: true,
    yijian: "",
    userInfo: {},
    hasUserInfo: false,
    phone:"",
    worklist:[],
    haveOne:false,
    scrollTop:0,
    hiddenModal: true,
    hiddenword: true,
    spell: '',
    wordname: 'Loading',
    wordcontent: '',
    wordDe: '',
    wordsamples: '',
  },
  // 事件处理函数
  toWork(){
    wx.showModal({
      title: '作业内容',
      content: this.data.worklist.content,
      showCancel:false,
      success: function(res) {
      console.log('用户点击确定')}
    })
  },
  gotoWork(){
    console.log(this.data.worklist.title)
    var Title=this.data.worklist.tittle;
    var id=this.data.worklist.workId;
    var descripte=this.data.worklist.description;
    var content=this.data.worklist.content;
    var type=this.data.worklist.type;
    var status=this.data.worklist.status;
    console.log(status)
    // console.log('index:'+item)
      wx.navigateTo({
      url: '/pages/work/work?title='+Title+'&id='+id+'&description='+descripte+'&content='+content+'&status='+status,
    })
  },
  getword() {
    this.setData({
      hiddenword: !this.data.hiddenword
    })
  },
  quit(){
    this.setData({
      phone:"",
      worklist:[],
      haveOne:false
    })
    wx.clearStorageSync();
    // this.onReady();
  },
  onPageScroll: function (e) {
    var _this = this;
    // console.log(e.scrollTop)
    _this.setData({
    scrollTop:e.scrollTop
    })
  },
  toAboutus(){
   wx.navigateTo({
     url: '/pages/aboutus/aboutus',
   })
  },

  toList (){
   wx.switchTab({
     url: '/pages/workList/workList',
   })
  },
 
  toNewwork(){
    if(this.data.phone===""){
      wx.showModal({
        title: '提示',
        content: '请先登录',
        showCancel:false,
        success: function(res) {
        console.log('用户点击确定')}
      })
    }
    else{
     wx.navigateTo({
         url: '/pages/NewWorkImform/NewWorkImform',
      })
    }
  },

  toSearch() {
   wx.navigateTo({
     url: '/pages/search/search',
   })
  },

  getUserInfo: function (e) {
    // wx.setStorageSync('phone', 'ozRu65DCNr2_G8RYQqOvNE_IDXMg');
    var that=this;
    wx.login({
      success: res => { 
        //  console.log(res.code);
         var code=res.code;
        wx.request({
          url: 'http://106.15.67.178:8080/oauth',
          data: {
            'code': res.code
          },
          method: 'GET',
          header: {
            'content-type': 'application/json'
          }, // 设置请求的 header
          success: function (res) {
            console.log(res)
            console.log("----调用成功----");
            if(res.statusCode==200){
              var Phone=res.data.openid;
              console.log(Phone);
              that.setData({
                hasUserInfo: true,
                phone:Phone
               })
               wx.setStorageSync('phone', Phone);
               wx.showModal({
                title: '提示',
                content: '成功登录',
                showCancel:false,
                success: function(res) {
                console.log('用户点击确定')}
              })
              // that.getWorkList();
            }
          },
          fail:function(abc){
            console.log("----调用失败----")
            console.log(abc)
               wx.showModal({
                title: '提示',
                content: '登录失败',
                showCancel:false,
                success: function(res) {
                console.log('用户点击确定')}
              })
          },
          complete:function(asd){
            console.log("----调用结束----")
          }
        }) 
      }
    })
  },

  getWorkList(){
    var that=this;
    wx.request({
      url: 'http://106.15.67.178:8080/getWorklist?ticket='+this.data.phone,
      // 18773392767
      method:'GET',
        success:res=>{
          // console.log(res);
          var listarr=res.data;
          var index=0,haveOne=false; 
          if(listarr){
          for(var i = 0; i < listarr.length; i++){
            if(listarr[i].status==0){
              index=i;
              haveOne=true;
              break;
            }
          };
          console.log(index);
          if(haveOne){
          that.setData({
            worklist:listarr[index],
            haveOne:true
          })
          console.log(that.data.worklist)
          }
          else {
            console.log('暂无未完成作业')
          }
        }
        }
     })
  },

  userFeedBack(){
    this.setData({
      hiddenModal: !this.data.hiddenModal
  })
  },

 confirmM:function() {
    this.setData({
        hiddenModal: true
    })
   console.log("意见" + this.data.yijian); 
 },

 cancelM:function() {
    this.setData({
        hiddenModal: true
    })
 },

yijian: function (e) {
  this.setData({
    yijian: e.detail.value
  })
},

/**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var getPhone = wx.getStorageSync('phone');
    if(getPhone){
      this.setData ({
        phone:getPhone
      })
      console.log("同步 从本地缓存取出数据phone=" +getPhone);
    }
    let index=Math.floor(Math.random() * WORDLIST.length) ;
    this.setData({
      spell: WORDLIST[index].explain,
      wordname: WORDLIST[index].word,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var getPhone = wx.getStorageSync('phone');
    if(getPhone){
      this.getWorkList();
    }
  },
  // onShareAppMessage: function () {
  //   return {
  //     "title": "今天我在易背听完成了作业，快来看看吧！"
  //   }
  // },
  // onShareTimeline: function () {
  //   return {
  //     "title": '今天我在易背听完成了作业，快来看看吧！'
  //   }
  // },
})
const WORDLIST=[
  {
    word:'absencen',
    explain:'缺席'
  },
  {
    word:'apologize',
    explain:'道歉'
  },
  {
    word:'Asian',
    explain:'n. 亚洲人 adj. 亚洲的；亚洲人的'
  },
  {
    word:'attempt',
    explain:'尝试（可作名词也可作动词）'
  },
  {
    word:'comfort',
    explain:'v.&n. 安慰，使舒适（comfortable adj. 舒适的；comfortably adv. 舒适地）'
  },
  {
    word:'cousin',
    explain:'n. 堂兄弟姊妹；表兄弟姊妹'
  },
  {
    word:'essential',
    explain:'基本的，本质的'
  },
]
